package practice_0524;

import java.util.Random;

public class practice01 {
    public static void main(String[] args) {
        int[][] arr = new int[3][3];
        boolean flag = false;
        Random random = new Random();
        int tmp = 0;
        int count = 0;
//        for (int i = 0; i < arr.length; i++) {
//            for (int j = 0; j < arr[0].length; j++) {
//                tmp = random.nextInt(10);
//                while(if_in(arr,tmp)){
//                    tmp = random.nextInt(10);
//
//                }
//                arr[i][j] = tmp;
//
//            }
//        }
//        print(arr);
        while (true) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[0].length; j++) {
                    tmp = random.nextInt(10);
                    while(if_in(arr,tmp)){
                        tmp = random.nextInt(10);

                    }
                    arr[i][j] = tmp;

                }
            }
            int sum1 = arr[0][0] + arr[0][1] + arr[0][2];
            int sum2 = arr[1][0] + arr[1][1] + arr[1][2];
            int sum3 = arr[2][0] + arr[2][1] + arr[2][2];
            int sum4 = arr[0][1] + arr[1][1] + arr[2][1];
            int sum5 = arr[0][0] + arr[1][0] + arr[2][0];
            int sum6 = arr[0][2] + arr[1][2] + arr[2][2];
            int sum7 = arr[0][0] + arr[1][1] + arr[2][2];
            int sum8 = arr[0][2] + arr[1][1] + arr[2][0];
            flag = sum1 == sum2 && sum1 == sum3 && sum1 == sum4 && sum1 == sum5 && sum1 == sum6 &&
                    sum1 == sum7 && sum1 == sum8;
            System.out.println(++count);
            if (flag) {
                System.out.println("是数独，结果如下：");
                break;
            }
        }
    print(arr);

    }

    public static boolean if_in(int[][] arr,int key) {
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                if (arr[i][j] == key) {
                    flag = true;
                }
            }
        }
        return flag;
    }
    public static void  print(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j] +" ");
            }
            System.out.println();
        }

    }



}
